<template>
	<div class="mod-shop">
		<el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
			<el-form-item>
				<el-input v-model="dataForm.name" placeholder="名称" clearable></el-input>
			</el-form-item>
			<el-form-item>
				<el-button @click="getDataList()">查询</el-button>
				<el-button v-if="isAuth('ad:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button>
				<el-button v-if="isAuth('ad:delete')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
			</el-form-item>
		</el-form>
		<el-table :data="dataList" border stripe v-loading="dataListLoading" @selection-change="selectionChangeHandle"
		 :height=tableHeight style="width: 100%;">
			<el-table-column type="expand">
				<template slot-scope="props">
					<el-form label-position="right" inline class="demo-table-expand">
						<el-form-item label="内容:">
							<span>{{ props.row.content }}</span>
						</el-form-item>
						<el-form-item label="链接:">
							<span>{{ props.row.link }}</span>
						</el-form-item>
					</el-form>
				</template>
			</el-table-column>
			<el-table-column type="selection" header-align="center" align="center" width="50">
			</el-table-column>
			<el-table-column prop="id" header-align="center" align="center" label="ID" width="70">
			</el-table-column>
			<el-table-column prop="imageUrl" header-align="center" align="center" label="预览" width="80">
				<template slot-scope="scope">
					<img :src="scope.row.imageUrl" class="picture" :preview="scope.row.id" v-if="scope.row.mediaType == 1"/>
					<div class="video" v-else @click="showZvideo(scope.row.imageUrl)"><img src="" :id="'id'+scope.row.id" class="picture"/></div>
				</template>
			</el-table-column>
			<el-table-column prop="adPositionName" header-align="center" align="center" label="广告位置">
			</el-table-column>
			<el-table-column prop="name" header-align="center" align="center" label="名称">
			</el-table-column>
			<el-table-column prop="mediaType" header-align="center" align="center" label="媒体类型" width="80">
				<template slot-scope="scope">
					<el-tag v-if="scope.row.mediaType == 2" size="small" type="danger">视频</el-tag>
					<el-tag v-else-if="scope.row.mediaType == 1" size="small">图片</el-tag>
				</template>
			</el-table-column>
			<el-table-column prop="sortOrder" header-align="center" align="center" label="排序" width="80">
			</el-table-column>
			<el-table-column prop="enabled" header-align="center" align="center" label="状态" width="80">
				<template slot-scope="scope">
					<el-tag v-if="scope.row.enabled == 1" size="small">正常</el-tag>
					<el-tag v-else size="small" type="danger">禁用</el-tag>
				</template>
			</el-table-column>
			<el-table-column prop="endTime" header-align="center" align="center" label="结束时间" width="150">
			</el-table-column>
			<el-table-column fixed="right" header-align="center" align="center" label="操作" width="100">
				<template slot-scope="scope">
					<el-button v-if="isAuth('ad:update')" type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
					<el-button v-if="isAuth('ad:delete')" type="text" size="small" @click="deleteHandle(scope.row.id)" style="color:#F56C6C">删除</el-button>
				</template>
			</el-table-column>
		</el-table>
		<el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
		 :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage" layout="total, sizes, prev, pager, next, jumper">
		</el-pagination>
		<!-- 弹窗, 新增 / 修改 -->
		<add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList" @closeDialog="addOrUpdateVisible=false"></add-or-update>
		<!-- 视频预览 -->
		<z-video v-if="zvideoVisible" :url="preVideoUrl" @closeDialog="zvideoVisible=false"></z-video>
	</div>
</template>

<script>
	import AddOrUpdate from './ad-add-or-update'
	import { getFrameImg } from '@/utils'
	import ZVideo from '@/components/z-video'
	export default {
		data() {
			return {
				dataForm: {
					name: ''
				},
				dataList: [],
				pageIndex: 1,
				pageSize: 10,
				totalPage: 0,
				dataListLoading: false,
				dataListSelections: [],
				addOrUpdateVisible: false,
				zvideoVisible: false,
				preVideoUrl: ''
			}
		},
		components: {
			AddOrUpdate,
			ZVideo
		},
		computed: {
			tableHeight: function() {
				return this.$store.state.common.documentClientHeight - 268
			}
		},
		activated() {
			this.getDataList()
		},
		methods: {
			// 获取数据列表
			getDataList() {
				this.dataListLoading = true
				this.$http({
					url: this.$http.adornUrl('/back/ad/list'),
					method: 'get',
					params: this.$http.adornParams({
						'page': this.pageIndex,
						'limit': this.pageSize,
						'name': this.dataForm.name
					})
				}).then(({
					data
				}) => {
					if (data && data.code === 0) {
						this.dataList = data.data.list
						this.totalPage = data.data.totalCount
						this.getPreviewData()
					} else {
						this.dataList = []
						this.totalPage = 0
					}
					this.dataListLoading = false
				})
			},
			// 新增 / 修改
			addOrUpdateHandle(id) {
				this.addOrUpdateVisible = true
				this.$nextTick(() => {
					this.$refs.addOrUpdate.init(id)
				})
			},
			// 删除
			deleteHandle(id) {
				var ids = id ? [id] : this.dataListSelections.map(item => {
					return item.id
				})
				this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					this.$http({
						url: this.$http.adornUrl('/back/ad/delete'),
						method: 'post',
						data: this.$http.adornData(ids, false)
					}).then(({
						data
					}) => {
						if (data && data.code === 0) {
							this.$message({
								message: '操作成功',
								type: 'success',
								duration: 1500,
								onClose: () => {
									this.getDataList()
								}
							})
						} else {
							this.$message.error(data.msg)
						}
					})
				}).catch(() => {})
			},
			// 每页数
			sizeChangeHandle(val) {
				this.pageSize = val
				this.pageIndex = 1
				this.getDataList()
			},
			// 当前页
			currentChangeHandle(val) {
				this.pageIndex = val
				this.getDataList()
			},
			// 多选
			selectionChangeHandle(val) {
				this.dataListSelections = val
			},
			//获取视频预览图片
			getPreviewData(){
				var that = this;
				that.dataList.map(function(item){
					if(item.mediaType == 2){
						getFrameImg({url:item.imageUrl,width:300,height:200}).then( ret => {
							document.getElementById('id'+item.id).src = ret
						})
					}
				})
			},
			//视频预览
			showZvideo(url){
				this.zvideoVisible=true,
				this.preVideoUrl=url
			}
		}
	}
</script>

<style scoped>
	.picture {
		width: 3rem;
		height: 3rem;
	}
	.video:hover:before{
		content:"";
		background: rgba(0,0,0,0.6) url("")no-repeat center center/60% 60%;
		width: 3rem;
		height: 3rem;
		position: absolute;
	}
	.demo-table-expand {
		font-size: 0;
	}

	.demo-table-expand label {
		width: 100px;
		color: #99a9bf;
	}

	.demo-table-expand .el-form-item {
		margin-right: 0;
		margin-bottom: 0;
		width: 50%;
	}
</style>
